package util;

import  java.io.File;  
import  java.io.FileOutputStream;  
import  java.io.IOException;  
import  java.io.OutputStreamWriter;  
import  java.util.ArrayList;  
import  java.util.HashMap;  
import  java.util.List;  
import  java.util.Map;  
  
import  freemarker.template.Configuration;  
import  freemarker.template.Template;  
import  freemarker.template.TemplateException;  
  
public   class  Test {  
  
    /**  
     * @param args  
     */   
    public   static   void  main(String[] args) {  
        //System.out.println(System.getProperty("user.dir")+"============");   
        Configuration cfg = new  Configuration(Configuration.VERSION_2_3_23);  
        try  {  
            cfg.setClassForTemplateLoading(Test.class ,  "/template" ); //指定模板所在的classpath目录   
            Template t = cfg.getTemplate("entity.ftl" ); //指定模板   
            //FileOutputStream fos = new  FileOutputStream( new  File( "d:/Student.java")); //java文件的生成目录   
            FileOutputStream fos = new  FileOutputStream( new  File( "src/template/Student.java"));
              
            //模拟数据源   
            Map data = new  HashMap();  
            data.put("package" ,  "edu" ); //包名   
            data.put("className" ,  "Student" );  
              
            List pros = new  ArrayList();  
            Map pro_1 = new  HashMap();  
            pro_1.put("proType" , String. class .getSimpleName());  
            pro_1.put("proName" ,  "Name" );  
            pros.add(pro_1);  
              
            Map pro_2 = new  HashMap();  
            pro_2.put("proType" , String. class .getSimpleName());  
            pro_2.put("proName" ,  "sex" );  
            pros.add(pro_2);  
              
            Map pro_3 = new  HashMap();  
            pro_3.put("proType" , Integer. class .getSimpleName());  
            pro_3.put("proName" ,  "age" );  
            pros.add(pro_3);  
              
            data.put("properties" , pros);  
            t.process(data, new  OutputStreamWriter(fos, "utf-8" )); //   
            fos.flush();  
            fos.close();  
        } catch  (IOException e) {  
            e.printStackTrace();  
        } catch  (TemplateException e) {  
            e.printStackTrace();  
        }  
    }  
  
}  
